#!/usr/bin/perl
do 'config.pl';
use Archive::Zip qw( :ERROR_CODES );

$index_dir = $destination . "/";

sub parse_toc {
    my $filename = shift;
    my $toc      = shift;    ## get the .toc string
    my $return   = ();
    my @tmp_array;
    my @fname_array = split( /(\/|\.)/, $filename );
    $return->{'Timestamp'} = time();
    $return->{'Package'} = $fname_array[2];
    foreach my $line ( split( /\n/, $toc ) ) {
        if ( $line =~ /## Interface: (.*)/gi ) {
            $return->{'Interface'} = $1;
            next;
        }
        if ( $line =~ /## Version: (.*)/gi ) {
            $return->{'Version'} = $1;
            next;
        }
        if ( $line =~ /## Title: (.*)/gi ) {
            $return->{'Title'} = $1;
            next;
        }
        if ( $line =~ /## Notes: (.*)/gi ) {
            $return->{'Notes'} = $1;
            next;
        }
        if ( $line =~ /## Author: (.*)/gi ) {
            $return->{'Author'} = $1;
            next;
        }
        if ( $line =~ /## eMail: (.*)/gi ) {
            $return->{'eMail'} = $1;
            next;
        }
        if ( $line =~ /## URL: (.*)/gi ) {
            $return->{'URL'} = $1;
            next;
        }
        if ( $line =~ /## (Dependencies|RequiredDeps|Dependancies): (.*)/gi ) {
            @tmp_array = split( /,/, $2 );
            $return->{'Dependencies'} = [@tmp_array];
            next;
        }
        if ( $line =~ /## OptionalDeps: (.*)/gi ) {
            @tmp_array = split( /,/, $1 );
            $return->{'OptionalDeps'} = [@tmp_array];
            next;
        }
    }
    return $return;
}

opendir( DIR, $index_dir ) || die "Could not open $index_dir for reading: $!";
my @mods = readdir(DIR);
closedir(DIR);

my $modhash = ();

# get the index data structure from all the .toc files
foreach my $mod (@mods) {
    next unless $mod =~ /\.zip/gi;
    my $zip = Archive::Zip->new();
    unless ( $zip->read( $index_dir . $mod ) == AZ_OK ) {
        die "Could not read $index_dir$mod";
    }
    foreach my $file ( @{ $zip->{members} } ) {
        next unless $file->{fileName} =~ /\.toc/gi;
        $modhash->{$mod} =
          ( parse_toc( $file->{fileName}, $zip->contents( $file->{fileName} ) )
          );
        print "Reading .toc from $mod...\n";
    }
}

# print out the xml of the data structure
print "Writing index...\n";
my $xml = "";
$xml .= "<XML><Addons Path=\"http://$fqdn/$download_dir/\">";
foreach my $zipname ( keys %$modhash ) {
    $xml .= "<Addon>";
    $xml .= "<File>$zipname</File>";
    foreach my $key ( keys %{ $modhash->{$zipname} } ) {
        $value = $modhash->{$zipname}{$key};
        if ( ref $value eq "ARRAY" ) {
            $xml .= "<$key>";
            foreach my $item ( @{$value} ) {
                $item =~ s/\x0d//gi;
                $item =~ s/&/&amp;/gi;
                $xml .= "<Item>$item</Item>";
            }
            $xml .= "</$key>";
        }
        else {
            $value =~ s/\x0d//gi;
            $value =~ s/&/&amp;/gi;
            $xml .= "<$key>$value</$key>";
        }
    }
    $xml .= "</Addon>";
}
$xml .= "</Addons></XML>";

eval {
    open( FILE, "> $web_root/index.xml" )
      or die "Couldn't write to $web_root/index.xml! $!";
    print( FILE $xml );
    close(FILE);
};

